import { defineStore } from 'pinia'
import { ref } from 'vue'
import { getLoginUser } from '@/api/userController.ts'

export const useLoginUserStore = defineStore('loginUser', () => {
  // 默认值
  const loginUser = ref<API.LoginUserVO>({
    userName: '未登录',
  })

  // 登录弹出框状态
  const showLoginModal = ref(false)
  
  // 注册弹出框状态
  const showRegisterModal = ref(false)

  // 获取登录用户信息
  async function fetchLoginUser() {
    const res = await getLoginUser()
    if (res.data.code === 0 && res.data.data) {
      loginUser.value = res.data.data
    }
  }
  
  // 更新登录用户信息
  function setLoginUser(newLoginUser: any) {
    loginUser.value = newLoginUser
  }

  // 显示登录弹出框
  function openLoginModal() {
    showLoginModal.value = true
    showRegisterModal.value = false // 关闭注册框
  }

  // 隐藏登录弹出框
  function closeLoginModal() {
    showLoginModal.value = false
  }

  // 显示注册弹出框
  function openRegisterModal() {
    showRegisterModal.value = true
    showLoginModal.value = false // 关闭登录框
  }

  // 隐藏注册弹出框
  function closeRegisterModal() {
    showRegisterModal.value = false
  }

  return { 
    loginUser, 
    showLoginModal,
    showRegisterModal,
    setLoginUser, 
    fetchLoginUser,
    openLoginModal,
    closeLoginModal,
    openRegisterModal,
    closeRegisterModal
  }
})
